package data.preprocessing;

import java.util.HashMap;
import java.util.Map;

import net.sf.snowball.ext.porterStemmer;;

public class EnglishPorterStemmer implements StringProcessor {
	protected porterStemmer stmmer;
	protected Map<String,String> word2stem ;

	public EnglishPorterStemmer() {

		stmmer = new porterStemmer();
		word2stem = null ;
		

	}

	public EnglishPorterStemmer(Map<String, String> m) {

		stmmer = new porterStemmer();
		word2stem=m ;

	}

	public String map(String from) {
		StringBuffer sb = new StringBuffer();
		String[] result = from.split("[^a-zA-Z0-9_']+");

		for (int i = 0; i < result.length; i++)
		{
			
			if(this.word2stem==null) {
				stmmer.setCurrent(result[i]);
				stmmer.stem();
				sb.append(stmmer.getCurrent());
				sb.append(" ");
			} else { /* Si le word2Stem existe, on va le completer */
				String key = result[i] ;
				stmmer.setCurrent(key);
				stmmer.stem();
				String value=stmmer.getCurrent() ;
				sb.append(value);
				sb.append(" ");
				this.word2stem.put(key,value) ;
			}

		}

		return (sb.toString());

	}
	
	
	public static void main(String args[]){
		String text;
		text = "ids is eye UN-FRIENDLY credible excellent-good great , bad poor unworthy , excel regret. 5-start generative general not";
		EnglishPorterStemmer sp = new EnglishPorterStemmer();
		HashMap<String,String> m = new HashMap<String,String>() ;
		System.out.println(sp.map(text)) ;
		System.out.println(m);
	}
}